<?php
declare(strict_types=1);

namespace App\Service;

use App\Helper\Log;
use App\Helper\Util;
use Hyperf\DbConnection\Db;

/**
 * 场馆服务
 */
class PlaceService 
{
    /**
     * [__construct description] 构造函数
     */
    public function __construct()
    {}
    /**
     * 场馆保存
     * [placeSave description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function placeSave($params)
    {
        if(!empty($params['id'])){
            $save_data = [
                'updated_at'=>time()
            ];
            if(!empty($params['name'])){
                $save_data['name'] = $params['name'];
            }
            if(!empty($params['province_id'])){
                $save_data['province_id'] = $params['province_id'];
            }
            if(!empty($params['city_id'])){
                $save_data['city_id'] = $params['city_id'];
            }
            if(!empty($params['sort'])){
                $save_data['sort'] = $params['sort'];
            }
            if(isset($params['status'])){
                $save_data['status'] = $params['status'];
            }
            Db::table('place')
                ->where(['id'=>$params['id']])
                ->update($save_data);
        }else{
            $save_data = [
                'name'=>$params['name'],
                'province_id'=>$params['province_id'],
                'city_id'=>$params['city_id'],
                'sort'=>$params['sort']??0,
                'status'=>$params['status']??0,
                'created_at'=>time()
            ];
            Db::table('place')
                ->insert($save_data);
        }
        return true;
    }
    /**
     * 根据ID获取名称
     * [getNameById description]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getNameById($id)
    {
        $name = Db::table('place')
            ->where(['id'=>$id])
            ->value("name");
        return $name;
    }
}
